<?php
// +----------------------------------------------------------------------
// | 简易CMS
// +----------------------------------------------------------------------
// | Copyright (C) 2010 Hoping Software Studio.
// +----------------------------------------------------------------------
// | Version $Id$
// +----------------------------------------------------------------------

namespace CMS\taglib;

use think\template\TagLib;

/**
 * 文档模型标签库
 */
class Article extends TagLib {

    /**
     * 定义标签列表
     *
     * @var array
     */
    protected $tags = array(
        'list'     => array(
            'attr'  => 'name,category,aids,page,row,field',
            'close' => 1
        ),
        'prev'     => array(
            'attr'  => 'name,info',
            'close' => 1
        ), // 获取上一篇文章信息
        'next'     => array(
            'attr'  => 'name,info',
            'close' => 1
        ), // 获取下一篇文章信息
        'page'     => array(
            'attr'  => 'cate,listrow',
            'close' => 0
        ), // 列表分页
        'position' => array(
            'attr'  => 'pos,cate,limit,filed,name',
            'close' => 1
        ), // 获取推荐位列表
    );

    /* 推荐位列表 */

    public function _position($tag, $content) {
        $pos   = intval($tag ['position']);
        $cate  = $tag ['cate']? : 0;
        $limit = empty($tag ['limit']) ? 'null' : $tag ['limit'];
        $field = empty($tag ['field']) ? 'true' : $tag ['field'];
        $name  = $tag ['name'];

        $parse = '<?php ';
        $parse .= '$' . $name . ' = D(\'ArticleTitle\')->position(';
        $parse .= $pos . ',';
        $parse .= $cate . ',';
        $parse .= $limit . ',';
        $parse .= $field . ');';
        $parse .= '$upDriver = C(\'upload.driver\');$upConfig = C(\'upload.\'.$upDriver.\'_config\');$upurl = $upConfig[\'urlpre\'];';
        $parse .= 'if($' . $name . '):foreach($' . $name . ' as &$v):$v[pic]=$upurl.\'/article/\'.$v[pic];endforeach;endif;';
        $parse .= ' ?>';
        $parse .= $content;
        return $parse;
    }

    /* 列表数据分页 */

    public function _page($tag) {
        $cate    = $tag ['cate'];
        $listrow = $tag ['listrow'];
        $parse = '<?php ';
        $parse .= '$count = D("ArticleTitle")->where(array("category_id"=>' . $cate . '))->count();';
        $parse .= '$__PAGE__ = new \CMS\Page($count, ' . $listrow . ');';
        $parse .= 'echo $__PAGE__->show();';
        $parse .= ' ?>';
        return $parse;
    }

    /* 获取下一篇文章信息 */

    public function _next($tag, $content) {
        $name  = $tag ['name'];
        $info  = $tag ['info'];
        $parse = '<?php ';
        $parse .= '$' . $name . ' = D(\'Document\')->next($' . $info . ');';
        $parse .= ' ?>';
        $parse .= '<notempty name="' . $name . '">';
        $parse .= $content;
        $parse .= '</notempty>';
        return $parse;
    }

    /* 获取上一篇文章信息 */

    public function _prev($tag, $content) {
        $name  = $tag ['name'];
        $info  = $tag ['info'];
        $parse = '<?php ';
        $parse .= '$' . $name . ' = D(\'Document\')->prev($' . $info . ');';
        $parse .= ' ?>';
        $parse .= '<notempty name="' . $name . '">';
        $parse .= $content;
        $parse .= '</notempty>';
        return $parse;
    }

    public function _list($tag, $content) {
        $name  = $tag ['name'];
        $cate  = $tag ['category'];
        $row   = empty($tag ['row']) ? '10' : $tag ['row'];
        $aids  = $tag['aids'];
        if (substr($aids, 0, 1) === '$') {
            $aids = $this->tpl->get(substr($aids, 1));
        }
        $parse = <<<EOT
<?php
\$upDriver = C('upload.driver');
\$upConfig = C('upload.'.\$upDriver.'_config');
\$upurl = \$upConfig['urlpre'];
\$__CATE__ = model('Category')->getChildrenId($cate);
\$model =  model('ArticleTitle')->page(I(C('VAR_PAGE', null, 'p'), 1, 'intval'),$row);
\$where = array();
\$aids = '$aids';
if(\$aids){
    \$where = array('id'=>array('IN', \$aids));
}
\$__LIST__ = \$model->lists(\$__CATE__, '`id` DESC', 1, \$where);
if(\$__LIST__) {
    \$aids = array();
    \$data = array();
    foreach(\$__LIST__ as \$row){
        if (\$row['pic']) {
            \$row['pic'] = \$upurl .'/article/'. \$row['pic'];
        }
        if (!\$row['author']) {
            \$row['author'] = \$row['username'];
        }
        \$aids[] = \$row['id'];
        \$data[\$row['id']] = \$row;
    }
    \$aids = arr2str(\$aids);
    \$result = D('Tags')->alias('a')
        ->join('tagslist b', 'b.tagid=a.tagid', 'LEFT')
        ->where("a.itemid IN (\$aids) AND a.tagtype='portal'")
        ->select();
    if (\$result) {
        foreach (\$result as \$v) {
            \$data[\$v['itemid']]['tags'][] = array('tag' => \$v['tagname'], 'tagid' => \$v['tagid']);
        }
    }
    \$result = \\think\\Db::name('Attachment')
        ->where('aid IN ('.\$aids.') AND aidtype="article" AND isimage=1')
        ->select();
    if (\$result) {
        foreach (\$result as \$v) {
            \$pic = \$upurl .'/article/' . \$v['attachment'];
            if (\$v['thumb']) {
                \$pic .= '.thumb.jpg';
            }
            \$data[\$v['aid']]['pics'][] = \$pic;
        }
        foreach (\$data as \$k => &\$v) {
            \$v['picnum'] = count(\$v['pics']);
            if (\$v['pic']) {
                if (\$v['picnum'] >= 8) {
                    \$v['pics'] = array_splice(\$v['pics'], 0, 8);
                } elseif (\$v['picnum'] >= 4) {
                    \$v['pics'] = array_splice(\$v['pics'], 0, 4);
                } elseif (\$v['picnum'] > 0) {
                    \$v['pics'] = array(\$v['pic']);
                }
            }
        }
    }
    \$__LIST__ = \$data;
}
?>
<volist name="__LIST__" id="$name">
$content
</volist>
EOT;
        return $parse;
    }

}
